<template>
  <el-dialog
    ref="refDlg"
    title="新增"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="800px"
    @close="closeDialog"
  >
    <div>
      <el-form ref="refForm" class="financeForm" :rules="formRules" :model="formData" size="mini" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="施工单位:" prop="unit">
              <input-with-dialog
                v-model="formData.unit"
                :props="{label:'unitName',value:'unitCode'}"
                :config="unitConfig"
                @change="unitChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人:" prop="principal">
              <span>{{ formData.principal }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人电话:" size="mini" prop="principalPhone">
              <span>{{ formData.principalPhone }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="办事处编码:" size="mini" prop="org">
              <input-with-dialog
                v-model="formData.org"
                :props="{label:'name',value:'code'}"
                :config="organizationConfig"
                @change="organizationChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="办事处名称:" size="mini">
              <span>{{ formData.officeName }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { submitUnitOffice } from '@/api/constructionUnitMng'
export default {
  name: 'AddUnitOffice',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      readonly: false,
      storeList: [],
      customer: [],
      formData: {
        org: [],
        unit: [],
        enable: 1
      },
      formRules: {
        unit: [
          { required: true, message: '请选择施工单位' }
        ],
        org: [
          { required: true, message: '请选择办事处' }
        ]
      },
      unitConfig: {
        title: '施工单位',
        interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/configConstUnit/page`,
        setting: [
          {
            prop: 'unitCode',
            title: '编码'
          },
          {
            prop: 'unitName',
            title: '名称'
          },
          {
            prop: 'principal',
            title: '负责人'
          },
          {
            prop: 'principalPhone',
            title: '负责人电话'
          }
        ],
        filtersRaw: [{ 'id': 'cooperationState', 'property': 'cooperationState', 'operator': '<>', 'value': 'D' }]
      },
      organizationConfig: {
        title: '办事处',
        interfacePath: `/select-service/api/v1/organization/page`,
        setting: [
          {
            prop: 'code',
            title: '编码'
          },
          {
            prop: 'name',
            title: '名称'
          }
        ],
        filtersRaw: [{ 'id': 'isOffice', 'property': 'isOffice', 'value': '1', 'operator': '=' }]
      }
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
  },

  methods: {
    // 选择施工单位
    unitChange(data) {
      this.formData.unitCode = data[0].unitCode
      this.formData.unitName = data[0].unitName
      this.formData.principal = data[0].principal
      this.formData.principalPhone = data[0].principalPhone
    },
    // 选择办事处
    organizationChange(data) {
      this.formData.officeCode = data[0].code
      this.formData.officeName = data[0].name
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    // 清空表单
    clearForm(done) {
      this.formData = {
        unit: [],
        org: []
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    // 提交表单处理
    submitForm() {
      if (this.submiting) return
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          this.submiting = true
          submitUnitOffice(this.formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.closeDialog()
              // const isNew = !this.formData.id // 是否新增数据 true (新增则刷新列表到第一页)/ false (非新增则刷新当前页)
              this.$emit('success')
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.img-upload-container {
  width: fit-content;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  .icon-msg{
    margin-left: 10px;
    color: #999;
  }
}
</style>
